<?php
class Add_item_model extends CI_Model{
	function __construct(){
		parent::__construct();
	}
	public function add_item($is_file, $file_name){
		$addCategory = $this->input->post('addCategory');
		$addManufacturer = $this->input->post('addManufacturer');
		$updateItem = $this->input->post('updateItem');
		$this->load->model('manufacturer_model');
		$this->load->model('category_model');
		$this->load->model('inventory_model');
		$this->load->model('promotion_model');
		$this->load->model('item_model');
		$this->load->model('price_rule_model');
		$error = array(
				'success_count' => 0
		);

		if ($is_file === 1){
			$file_content = read_file('./uploads/items/'.$file_name);
			$lines = preg_split('/\n+/',$file_content);
			$type = 1;
			foreach ($lines as $line){
				if (strrpos($line, ':') === false)
					break;
				list($name, $category, $manufacturer, $barcode, $cost,
						$curr_stock, $min_stock, $bundle_unit) = explode(":",$line);

				$item = array(
						'name' => $name,
						'barcode' => $barcode,
						'type' => $type,
						'category' => $category,
						'manufacturer' => $manufacturer,
						'cost' => $cost,
						'price' => $cost * 1.2, 
						'curr_stock' => $curr_stock,
						'min_stock' => $min_stock,
						'bundle_unit' => $bundle_unit,
						'active' => 1,
						'error_message' => ''
				);
				$error = $this->add($addCategory, $addManufacturer, $updateItem, $item, $error);
			}
		}


		$count = 0;
		while ($this->input->post('name_'.$count) != null ){
			$name = $this->input->post('name_'.$count);
			$barcode = $this->input->post('barcode_'.$count);
			$type = $this->input->post('type_'.$count);
			$category = $this->input->post('category_'.$count);
			$manufacturer = $this->input->post('manufacturer_'.$count);
			$cost = $this->input->post('cost_'.$count);
			$price = $this->input->post('price_'.$count);
			$curr_stock = $this->input->post('curr_stock_'.$count);
			$min_stock = $this->input->post('min_stock_'.$count);
			$bundle_unit = $this->input->post('bundle_unit_'.$count);
			$active = 1;

			$item = array(
					'name' => $name,
					'barcode' => $barcode,
					'type' => $type,
					'category' => $category,
					'manufacturer' => $manufacturer,
					'cost' => $cost,
					'price' => $price,
					'curr_stock' => $curr_stock,
					'min_stock' => $min_stock,
					'bundle_unit' => $bundle_unit,
					'active' => 1,
					'error_message' => ''
			);

			$error = $this->add($addCategory, $addManufacturer, $updateItem, $item, $error);
			$count++;
		}

		return $error;
	}

	private function add($addCategory, $addManufacturer, $updateItem, $item, $error){

		if (!$this->validate_input($item)){
			$item['error_message'] = Messages::invalid_input;
			array_push($error, $item);
			return $error;
		}

		$success = true;

		$name = element('name' , $item);
		$barcode = element('barcode' , $item);
		$type = element('type' , $item);
		$category = element('category' , $item);
		$manufacturer = element('manufacturer' , $item);
		$cost = element('cost' , $item);
		$price = element('price' , $item);
		$curr_stock = element('curr_stock' , $item);
		$min_stock = element('min_stock' , $item);
		$bundle_unit = element('bundle_unit' , $item);
		$active = element('active' , $item);


		if (strlen($barcode)!=8){
			$item['error_message'] =Messages::invalid_barcode;
			array_push($error, $item);
			return $error;
		}

		$manufacturer_id = $this->manufacturer_model->get_id($manufacturer);
		if ($manufacturer_id == -1){
			if ($addManufacturer){
				$manufacturer_id = $this->manufacturer_model->add($manufacturer, $active);
				if ($manufacturer_id == -1){
					$item['error_message'] = Messages::fail_to_create_manufacturer;
					array_push($error, $item);
					return $error;
				}
			}else{
				$item['error_message'] = Messages::not_found_manufacturer;
				array_push($error, $item);
				return $error;
			}
		}
			
		$category_id = $this->category_model->get_id($category, $active);
		if ($category_id == -1){
			if ($addCategory){
				$category_id = $this->category_model->add($category);
				if ($category_id == -1){
					$message = array ('error_message' => Messages::fail_to_create_category);
					array_replace ($item, $message);
					array_push($error, $item);
					return $error;
				}
			}else{
				$item['error_message'] = Messages::not_found_category;
				array_push($error, $item);
				return $error;
			}
		}
			
		if ($success == true){
			$itemRow = $this->item_model->get_item_by_barcode($barcode);
				
			$discount = $this->price_rule_model->get_discount(($curr_stock-$min_stock)/$min_stock*100);

			if ($itemRow == null){
				$item_id = $this->item_model->add($name, $barcode, $type, $category_id, $manufacturer_id, $cost, $price, $active);
				if ($item_id != -1){

					$this->inventory_model->add($item_id, $curr_stock, $min_stock);
					if ($bundle_unit > 0){
						$this->promotion_model->add($item_id, $bundle_unit, $discount, $active);
					}else
						$this->promotion_model->add($item_id, 1, $discount, $active);
					$error['success_count']++;
				}else{
					$item['error_message'] = Messages::fail_to_create_item;
					array_push($error, $item);
					return $error;
				}
			}else {
				if ($updateItem){
					// DO update item
					$item_id = $itemRow->item_id;
					$this->item_model->update($name, $barcode, $type, $category_id, $manufacturer_id, $cost, $price, $active);
					$this->inventory_model->update($item_id, $curr_stock, $min_stock);
					if ($bundle_unit > 0){
						$this->promotion_model->update($item_id, $bundle_unit, $discount, $active);
					}else
						$this->promotion_model->update($item_id, 1, $discount, $active);
					$error['success_count']++;
				}else {
					$item['error_message'] = Messages::duplicate_barcode;
					array_push($error, $item);
					return $error;
				}
			}
		}else{
			$item['error_message'] = Messages::invalid_input;
			array_push($error, $item);
			return $error;
		}
		return $error;
	}

	private function validate_input($item){
		$name = element('name' , $item);
		$barcode = element('barcode' , $item);
		$type = element('type' , $item);
		$category = element('category' , $item);
		$manufacturer = element('manufacturer' , $item);
		$cost = element('cost' , $item);
		$curr_stock = element('curr_stock' , $item);
		$min_stock = element('min_stock' , $item);
		$bundle_unit = element('bundle_unit' , $item);
		$active = element('active' , $item);

		if (strlen($name) == null || strlen($name) < 0){
			return false;
		}
		if (!is_numeric($barcode) ){
			return false;
		}
		if (!is_numeric($type) || $type < 0){
			return false;
		}
		if (strlen($category) == null || strlen($category) < 0){
			return false;
		}
		if (strlen($manufacturer) == null || strlen($manufacturer) < 0){
			return false;
		}
		if (!is_numeric($cost) || $cost < 0){
			return false;
		}
		if (!is_numeric($curr_stock) || $curr_stock < 0){
			return false;
		}
		if (!is_numeric($min_stock) || $min_stock < 0){
			return false;
		}
		if (!is_numeric($bundle_unit) || $bundle_unit < 0){
			return false;
		}
		if (!is_numeric($active) || $active < 0){
			return false;
		}
		return true;
	}
}